<?php namespace Common\Model;
use Hdphp\Model\Model;

class Commodlist extends Model{
    protected $table='commodlist';

    protected $validate=array(
//        array('cogroupid[]','checkcogroup','规格不能为空',3,3),
//        array('colnum','required','库存不能为空',3,3),
        array('colstock','required','货号不能为空',3,3),

    );


    public function store(){
//        p(Q('post.cogroupid'));die;
        if(!$this->create()) return false;
        foreach(Q('post.cogroupid') as $u){
            if($u==0) { $this->error='规格不能为空'; return false;}
        }
        $coid=$_SESSION['info']['coid'];
        $cogroupid=implode(',',Q('post.cogroupid'));
        $cogroupidData=$this->where("shop_commodity_coid={$coid} AND cogroupid='{$cogroupid}'")->find();
//        p($cogroupidData);die;
        if($cogroupidData){$this->error='不能添加相同的规格条件'; return false;}
//        p($cogroupidData);die;
        $colstock=Q('post.colstock');
        $colstockData=$this->where("colstock={$colstock}")->find();
        if($colstockData){$this->error='不能添加相同货号的货品'; return false;}
        $this->data['cogroupid']=$cogroupid;
        $this->add();
        return true;
    }

    public function edit(){
        if(!$this->create())return false;
        $cogroupid=implode(',',Q('post.cogroupid'));
        $this->data['cogroupid']=$cogroupid;
//        p($this->data['colid']);die;
        $this->save();
//        p($_SESSION['info']['coid']);die;
        return true;
    }
}

